#include <prot.h>
#include "basic.h"

BOLT(utool_dscp, "utool <id> dscp <dscp>")
{
  uint8_t dscp = 0;
  uint32_t id;
 
  const char *argz = pop_argz();
  if (argz == NULL || !str2u8(argz, &dscp)) {
      printf("invalid DSCP\n");
      return;
  }
  if (dscp < 0 || dscp > 63) {
      printf("invalid DSCP, range[0~63]\n");
      return;
  }    
  argz = pop_argz();
  if (argz == NULL || !str2u32(argz, &id)) {
      printf("invalid id\n");
      return;
  }
  if (utool_set_dscp((int)id, dscp) == 0)
      printf ("Set dscp successfully.\n");
  else
      printf ("Set dscp failure.\n");
}

BOLT(utool_show, "utool show")
{
   utool_show();
}

BOLT(utool_send, "utool <id> send <number>")
{
   uint32_t num, id;
   int ret;
   
   if (!str2u32(pop_argz(), &num)){
      printf("invalid number\n");
      return;
   } 
   if (!str2u32(pop_argz(), &id)){
      printf("invalid id\n");
      return;
   }
   for(ret=0;num>0;num--) {
      if (utool_send((int )id) > 0)
         ret++;
      usleep(1);
   }
   printf("utool send num=%d\n", ret);
}

BOLT(utool_release, "utool <id> release")
{
   uint32_t id;
   if (!str2u32(pop_argz(), &id)){
      printf("invalid id\n");
      return;
   } 
   utool_release((int)id);
}

BOLT(ut_create, "utool <id> create <src-ip> <src-port> <dst-ip> <dst-port>")
{
   uint32_t ut_id;
   sockaddr_t src, dst;
   int id;
   uint16_t port;
   
   memset(&src, 0, sizeof(src));
   memset(&dst, 0, sizeof(dst));
   if (!str2u16(pop_argz(), &port)){
      printf("invalid destination port\n");
      return;
   } 
   if (!ip2addr(pop_argz(), &dst.v0)) {
      printf ("Invalid destination IP.\n");
      return;
   }
   addr_set_port(&dst, port);
   if (!str2u16(pop_argz(), &port)){
      printf("invalid source port\n");
      return;
   } 
   if (!ip2addr(pop_argz(), &src.v0)) {
      printf ("Invalid source IP.\n");
      return;
   }
   addr_set_port(&src, port);
   if (!str2u32(pop_argz(), &ut_id)){
      printf("invalid id\n");
      return;
   } 
   id=utool_create((int)ut_id, &src, &dst);
   printf("The utool id is %d\n", id);
}

BOLT(utool_rate, "utool <id> rate <rate>")
{
   uint32_t ut_id, rate;

   if (!str2u32(pop_argz(), &rate)){
      printf("invalid rate limit\n");
      return;
   } 
   if (!str2u32(pop_argz(), &ut_id)){
      printf("invalid id\n");
      return;
   } 
   utool_rate((int)ut_id, rate);
}




